import { insertAfter } from "./public";

// 谷歌翻译的回调函数
export function TranslateGoogleCnCallBack() {
  // 复制功能
  function DownloadFunction(inputVal) {
    var aux = document.createElement("input");
    aux.setAttribute("value", inputVal);
    document.body.appendChild(aux);
    aux.select();
    document.execCommand("copy");
    document.body.removeChild(aux);
  }
  // 开头字母转大写
  function startWithUppercase(s) {
    return s
      .toLowerCase()
      .split(/\s+/)
      .reduce(function (prev, item) {
        return (
          prev +
          (prev.trim() && " ") +
          item.slice(0, 1).toUpperCase() +
          item.slice(1)
        );
      }, "");
  }
  // 开头字母转小写
  function startWithLowercase(s) {
    return s
      .toLowerCase()
      .split(/\s+/)
      .reduce(function (prev, item) {
        return (
          prev +
          (prev.trim() && " ") +
          item.slice(0, 1).toLowerCase() +
          item.slice(1)
        );
      }, "");
  }

  setTimeout(function () {
    // 中译英
    var spanEl = document.createElement("span");
    spanEl.innerHTML = "中译英";
    spanEl.className = "boxue_translateGoogle_button";
    spanEl.onclick = () =>
      (window.location.href =
        "https://translate.google.com.hk/?hl=zh-CN&sourceid=cnhp&sl=zh-CN&tl=en&op=translate");
    insertAfter(spanEl, document.getElementsByClassName("cWQYBc")[1]);

    // 英译中
    var spanEl2 = document.createElement("span");
    spanEl2.innerHTML = "英译中";
    spanEl2.className = "boxue_translateGoogle_button";
    spanEl2.onclick = () =>
      (window.location.href =
        "https://translate.google.com.hk/?hl=zh-CN&sourceid=cnhp&sl=en&tl=zh-CN&op=translate");
    insertAfter(spanEl2, document.getElementsByClassName("cWQYBc")[1]);

    // 中划线
    var spanEl3 = document.createElement("span");
    spanEl3.innerHTML = "中划线";
    spanEl3.className = "boxue_translateGoogle_button";
    spanEl3.onclick = () => {
      let val = document.getElementsByClassName("VIiyi")[0].innerText;

      val = startWithLowercase(val); //转换开头字母小写
      val = val.replace(/-/g, " ");
      val = val.replace(/\s+/g, "-");
      val = val.substring(0, 1).toLowerCase() + val.substring(1);
      val = val.replace(/,/g, "");
      val = val.replace(/\./g, "");

      DownloadFunction(val);
    };
    insertAfter(spanEl3, document.getElementsByClassName("cWQYBc")[1]);

    // 下划线
    var spanEl4 = document.createElement("span");
    spanEl4.innerHTML = "下划线";
    spanEl4.className = "boxue_translateGoogle_button";
    spanEl4.onclick = () => {
      let val = document.getElementsByClassName("VIiyi")[0].innerText;

      val = startWithLowercase(val); //转换开头字母小写
      val = val.replace(/-/g, " ");
      val = val.replace(/\s+/g, "_");
      val = val.substring(0, 1).toLowerCase() + val.substring(1);
      val = val.replace(/,/g, "");
      val = val.replace(/\./g, "");

      DownloadFunction(val);
    };
    insertAfter(spanEl4, document.getElementsByClassName("cWQYBc")[1]);

    // 驼峰
    var spanEl5 = document.createElement("span");
    spanEl5.innerHTML = "驼峰";
    spanEl5.className = "boxue_translateGoogle_button";
    spanEl5.onclick = () => {
      let val = document.getElementsByClassName("VIiyi")[0].innerText;

      val = startWithUppercase(val); //转换开头字母大写
      val = val.replace(/-/g, " ");
      val = val.replace(/\s+/g, ""); //去掉空格
      val = val.substring(0, 1).toLowerCase() + val.substring(1);
      val = val.replace(/,/g, "");
      val = val.replace(/\./g, "");
      DownloadFunction(val);
    };
    insertAfter(spanEl5, document.getElementsByClassName("cWQYBc")[1]);

    // 全部大写+下划线
    var spanEl6 = document.createElement("span");
    spanEl6.innerHTML = "全大写(下划线)";
    spanEl6.className = "boxue_translateGoogle_button";
    spanEl6.onclick = () => {
      let inputVal = document.getElementsByClassName("VIiyi")[0].innerText;
      DownloadFunction(
        inputVal.toUpperCase().replace(/-/g, " ").replace(/\ /g, "_")
      );
    };
    insertAfter(spanEl6, document.getElementsByClassName("cWQYBc")[1]);

    console.log("您进入的是谷歌翻译页面 300毫秒");
  }, 500);
}
